﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using System.Data.Linq;
using CrystalDecisions.CrystalReports.Engine;
using QLTVNEW.Report;
using CrystalDecisions.Shared;

namespace QLTVNEW
{
    public partial class frmInDSach : DevComponents.DotNetBar.Office2007Form
    {
        public frmInDSach()
        {
            InitializeComponent();
            DataLoadOptions dlo = new DataLoadOptions();
            dlo.LoadWith<SACH>(s => s.THELOAI);
            dlo.LoadWith<SACH>(s => s.TACGIA1);
            dlo.LoadWith<SACH>(s => s.NHAXUATBAN1);
            dlo.LoadWith<SACH>(s => s.TINHTRANG);
            db.LoadOptions = dlo;
        }

        QLTVDataContext db = new QLTVDataContext();
        rptSach rpt = new rptSach();
        private void frmInDSach_Load(object sender, EventArgs e)
        {
            var query = (from sa in db.SACHes
                         join tg in db.TACGIAs on sa.TacGia equals tg.MaTacGia
                         join tbnxb in db.NHAXUATBANs on sa.NhaXuatBan equals tbnxb.MaNXB
                         join tbtr in db.TINHTRANGs on sa.MaTinhTrang equals tbtr.MaTinhTrang
                         join tbtl in db.THELOAIs on sa.MaTheLoai equals tbtl.MaTheLoai
                         select new
                         {
                             sa.MaSach,
                             sa.TenSach,
                             tbnxb.TenNXB,
                             tg.TenTacGia,
                             tbtl.TenTheLoai,
                             sa.NamXuatBan,
                             sa.NgayNhap,
                             tbtr.TenTinhTrang

                         }).ToList();

            //ConnectionInfo conn = new ConnectionInfo();
            //TableLogOnInfo tablelog = new TableLogOnInfo();
            //conn.ServerName = @".\sqlexpress1";
            //conn.IntegratedSecurity = true;
            //conn.DatabaseName = "QuanLyTV";

            //tablelog.ConnectionInfo = conn;

            //foreach (Table tablename in rpt.Database.Tables)
            //{
            //    tablename.ApplyLogOnInfo(tablelog);
            //}

            rpt.SetDataSource(query);
            this.crystalReportViewer1.ReportSource = rpt;

        }


        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void máyInToolStripMenuItem_Click(object sender, EventArgs e)
        {
            PrintDialog f = new PrintDialog();
            if (f.ShowDialog() == DialogResult.OK)
            {
                rpt.PrintToPrinter(1, true, -1, -1);
            }
        }

        private void fileWordToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog save = new SaveFileDialog();
            save.Title = "Save As";
            save.Filter = ".doc|*.doc";

            if (save.ShowDialog() == DialogResult.OK)
            {
                rpt.ExportToDisk(CrystalDecisions.Shared.ExportFormatType.WordForWindows, save.FileName);
            }
        }

        private void fileEecelToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog save = new SaveFileDialog();
            save.Title = "Save As";
            save.Filter = ".xlsx|*.xlsx";

            if (save.ShowDialog() == DialogResult.OK)
            {
                rpt.ExportToDisk(CrystalDecisions.Shared.ExportFormatType.Excel, save.FileName);
            }
        }

        private void filePDFToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog save = new SaveFileDialog();
            save.Title = "Save As";
            save.Filter = ".pdf|*.pdf";

            if (save.ShowDialog() == DialogResult.OK)
            {
                rpt.ExportToDisk(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat, save.FileName);
            }
        }
    }
}
